package com.ynding.cloud.auth.server.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynding.cloud.auth.api.admin.entity.MenuRole;
import com.ynding.cloud.auth.api.admin.entity.Role;
import com.ynding.cloud.auth.api.admin.entity.UserRole;
import com.ynding.cloud.auth.server.admin.dao.MenuRoleMapper;
import com.ynding.cloud.auth.server.admin.dao.RoleMapper;
import com.ynding.cloud.auth.server.admin.dao.UserRoleMapper;
import com.ynding.cloud.auth.server.admin.service.IRoleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p> </p>
 *
 * @author ynding
 * @version 2021/11/1
 **/
@Service
@Slf4j
@RequiredArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
        implements IRoleService {
    private final RoleMapper roleMapper;
    private final MenuRoleMapper menuRoleMapper;
    private final UserRoleMapper userRoleMapper;

    @Override
    public List<Role> listByMid(Long menuId) {
        List<MenuRole> menuRoles = menuRoleMapper.selectList(new LambdaQueryWrapper<MenuRole>()
                .eq(MenuRole::getMid, menuId));
        List<Role> roles = roleMapper.selectBatchIds(menuRoles.stream().map(MenuRole::getRid).collect(Collectors.toList()));
        return roles;
    }

    @Override
    public List<Role> listByUserid(Long userid) {
        List<UserRole> userRoles = userRoleMapper.selectList(new LambdaQueryWrapper<UserRole>()
                .eq(UserRole::getUserid, userid));
        List<Role> roles = roleMapper.selectBatchIds(userRoles.stream().map(UserRole::getRid).collect(Collectors.toList()));
        return roles;
    }
}
